package com.marakana.yamba4;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.chris.bean.Person;
import com.chris.weibo.R;
import com.marakana.yamba1.PrefsActivity;
import com.marakana.yamba1.StatusActivity2;
import com.marakana.yamba1.YambaApplication1;
import com.marakana.yamba3.UpdateService1;

public class BaseActivity extends Activity {

	YambaApplication1 yamba;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		yamba = (YambaApplication1) getApplication();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.itemPrefs:
			startActivity(new Intent(this, PrefsActivity.class)
					.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
			break;
		case R.id.itemToggleService:
			if (yamba.isServiceRunning()) {
				stopService(new Intent(this, UpdateService1.class));
			} else {
				startService(new Intent(this, UpdateService1.class));
			}
			break;
		case R.id.itemPurge:
			break;
		case R.id.itemTimeLine:
			startActivity(new Intent(this, TimeLineActivity2.class).addFlags(
					Intent.FLAG_ACTIVITY_SINGLE_TOP).addFlags(
					Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
			break;
		case R.id.itemStatus:
			startActivity(new Intent(this, StatusActivity2.class)
					.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
			break;
		case R.id.itemShowPrefs:
			Person person = ((YambaApplication1) getApplication()).getPerson();
			new AlertDialog.Builder(this)
					.setMessage(
							"username: " + person.getUsername() + "\n"
									+ "password: " + person.getPassword()
									+ "\n" + "ApiRoot: " + person.getApiRoot())
					.setPositiveButton("OK", null).show();
			break;
		}
		return true;
	}

	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {
		MenuItem toggleItem = menu.findItem(R.id.itemToggleService);
		if (yamba.isServiceRunning()) {
			toggleItem.setTitle(R.string.titleServiceStop);
			toggleItem.setIcon(android.R.drawable.ic_media_pause);
		} else {
			toggleItem.setTitle(R.string.titleServiceStart);
			toggleItem.setIcon(android.R.drawable.ic_media_play);
		}
		return true;
	}
}
